package com.jsm.twentyfour.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * Created jsm on 2018/1/24.
 */
public class DateUtil {
    static {
        TimeZone.setDefault(TimeZone.getTimeZone("GMT+08:00"));
    }

    private static final Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
    public static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final Logger log = LoggerFactory.getLogger(DateUtil.class);

    public static Date now() {
        return Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00")).getTime();
    }

    public static Date getDayEnd(Date date) {

        calendar.setTimeInMillis(date.getTime());
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }

    public static Date getDayStart(Date date) {
        calendar.setTimeInMillis(date.getTime());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        log.info("time:" + calendar.getTime());
        return calendar.getTime();
    }

    public static Date getTodyEnd() {
        return getDayEnd(now());
    }


    public static Date getDayStart() {
        return getDayStart(now());
    }


    public static Date before(int day) {
        return before(null, day);
    }

    public static Date beforeHour(Date date, int hour) {

        if (date == null) {
            date = getDayStart();
        }

        long current = date.getTime();
        long onehour = 60 * 60 * 1000;
        calendar.setTimeInMillis(current - (onehour * hour));
        return calendar.getTime();
    }

    public static Date afterMonth(Date date, int month) {
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
        cal.setTime(date);
        cal.add(cal.MONTH, 1);
        return cal.getTime();
    }

    public static Date before(Date date, int day) {
        if (date == null) {
            date = getDayStart();
        }
        long current = date.getTime();
        long oneday = 24 * 60 * 60 * 1000;
        calendar.setTimeInMillis(current - (oneday * day));
        return getDayStart(calendar.getTime());
    }

    public static String format(Date date) {
        return sdf.format(date);
    }

    public static int getBetweenDays(Date starttime, Date enditme) {
        long start = starttime.getTime();
        long end = enditme.getTime();
        return (int) ((end - start) / (1000 * 60 * 60 * 24) + 0.5);
    }

    public static Date StringToDate(String date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date newDate = null;
        try {
            newDate = sdf.parse(date);
            return newDate;
        } catch (ParseException e) {
            e.printStackTrace();
            return newDate;
        }
    }

    public static String format(Date date, String format) {
        if (format != null) {
            SimpleDateFormat format1 = new SimpleDateFormat(format);
            return format1.format(date);
        } else {
            return format(date);
        }
    }

    public static Date afterHour(Date date, int hour) {
        if (date == null) {
            date = now();
        }
        long current = date.getTime();
        long onehour = 60 * 60 * 1000;
        calendar.setTimeInMillis(current + (onehour * hour));
        return calendar.getTime();
    }

    public static Date beforeMinute(int minute) {
        long current = now().getTime();
        long oneMinute = 60 * 1000;
        calendar.setTimeInMillis(current + (minute * oneMinute));
        return calendar.getTime();
    }

    /**
     * 计算两个日期天数之差
     *
     * @param bigDate
     * @param smallDate
     * @return
     */
    public static int getDaysInterval(Date bigDate, Date smallDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(bigDate);
        int day1 = calendar.get(Calendar.DAY_OF_YEAR);
        calendar.setTime(smallDate);
        int day2 = calendar.get(Calendar.DAY_OF_YEAR);
        return day2 - day1;
    }

    /**
     * @return boolean
     * @Description 是否为当天24h内
     */
    public static boolean isToday(Date inputJudgeDate) {
        boolean flag = false;
        //获取当前系统时间
        long longDate = System.currentTimeMillis();
        Date nowDate = new Date(longDate);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(nowDate);
        String subDate = format.substring(0, 10);
        //定义每天的24h时间范围
        String beginTime = subDate + " 00:00:00";
        String endTime = subDate + " 23:59:59";
        Date paseBeginTime = null;
        Date paseEndTime = null;
        try {
            paseBeginTime = dateFormat.parse(beginTime);
            paseEndTime = dateFormat.parse(endTime);

        } catch (ParseException e) {
            log.error(e.getMessage());
        }
        if (inputJudgeDate.after(paseBeginTime) && inputJudgeDate.before(paseEndTime)) {
            flag = true;
        }
        return flag;
    }

    public static void main(String[] args) {
    }

    public static Date initDateByMonth() {
        calendar.setTime(now());
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();

    }

    //获取本周的开始时间
    public static Date getBeginDayOfWeek() {
        Date date = new Date();
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        cal.add(Calendar.DATE, 2 - dayofweek);
        return DateUtil.getDayStart(cal.getTime());
    }

    //获取本周的结束时间
    public static Date getEndDayOfWeek() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getBeginDayOfWeek());
        cal.add(Calendar.DAY_OF_WEEK, 6);
        Date weekEndSta = cal.getTime();
        return DateUtil.getDayEnd(weekEndSta);
    }

    public static String secToTime(int time) {
        String timeStr = null;
        int hour = 0;
        int minute = 0;
        int second = 0;
        if (time <= 0)
            return "0秒";
        else {
            minute = time / 60;
            if (minute < 60) {
                second = time % 60;
                timeStr = unitFormat(minute) + ":" + unitFormat(second);
            } else {
                hour = minute / 60;

                minute = minute % 60;
                second = time - hour * 3600 - minute * 60;
                timeStr = unitFormat(hour) + "时" + unitFormat(minute) + "分" + unitFormat(second) + "秒";
            }
        }
        return timeStr;
    }

    public static String unitFormat(int i) {
        String retStr = null;
        if (i >= 0 && i < 10)
            retStr = "0" + Integer.toString(i);
        else
            retStr = "" + i;
        return retStr;
    }

    public static Date getTime(Long time) {
        calendar.setTimeInMillis(time);
        return calendar.getTime();
    }
}
